<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
	contentType="text/html;charset=UTF-8"%>
<jsp:directive.page import="cn.antia.cep.busi.DbHibernateService" />
<jsp:directive.page import="cn.antia.cep.common.ContextBean" />
<jsp:directive.page import="cn.antia.cep.db.hibernate.bean.Role" />
<jsp:directive.page import="cn.antia.cep.right.RightTree" />
<jsp:directive.page import="cn.antia.cep.right.ResourceTreeBuilder" />
<jsp:directive.page import="cn.antia.cep.right.TreeNode" />
<jsp:directive.page import="cn.antia.cep.common.pool.AntiaPoolHelper"/>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">
		<title>role -- resource</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<script type="text/javascript" src="resource/js/xyTree/DivTree.js"></script>
		<script type="text/javascript" src="resource/js/xyTree/Tree.js"></script>
		<script type="text/javascript" src="resource/js/xyTree/Node.js"></script>
		<script type="text/javascript" src="resource/js/mootools.js"></script>
		<link rel="StyleSheet" href="resource/css/default.css" type="text/css" />
		<style type="text/css">
		body{
			background-color: #F9F8E8;
			font-family: arial;
			font-size: 12px;
		}
		
		tr{
			background-color: #F9F8E8;
		}
		a{
			font-size: 12px;
			font-family: arial;
			color: green;
		}
		a:HOVER {
			color: red;
		}
		a:VISITED {
			color: gray;
		}
		a:FOCUS {
			color: yellow;
		}
		.table_role{
			width:360px;
			font-size: 12px;
			font-family: arial;
			background-color: #000000;
		}
		.role_th{
			font-family: arial;
			font-weight: bold;
			color: #123234";
		}
	</style>
	</head>
	<script type="text/javascript">
  	var cur_role = 0;
  	var cur_row = null;
  	var resourceTree = null;
  	function changeRole(rid,roww){
  		if(resourceTree == null) return;
  		$('prompt_info').innerHTML = "操作正在执行...";
  		if(cur_row != null){
			cur_row.style.backgroundColor = "#F9F8E8";
  		}
		roww.style.backgroundColor = "#EEE999";
		cur_role = rid;
		cur_row = roww;
		//发送角色ID到后台，查询这个角色已经有的资源
		var paras = "action=query&roleId=" + rid;
		new Ajax("roleResource.was",{postBody:paras,onComplete:getRoleResources,encoding:'UTF-8'}).request();
		resourceTree.initReset()
  	}
  	function getRoleResources(request){
  		resourceTree.initReset()
  		var arr = resourceTree.tree.treeArray;
  		if(request != null && request != "null"){
  			var rids = request.split(",");
  			resourceTree.expandTree();
  			for(var i = 0;i<rids.length;i++){
  				var node  = findNode(arr,rids[i]);//得到节点
				if(node){
					var nele = node.getHtmlElementCheckboxImg();
					if(nele == null){
						alert("NOT FOUND:" + rids[i] + "NODE: " + node);
					}else{
						nele.selected();
					}
				}
  			}
  			resourceTree.initTreeForm();
  		}
  		$('prompt_info').innerHTML = "操作执行成功。";
  	}
  	 function findNode(arr,sid) {
	  for(var i=0;i<arr.length;i++){
	    if(arr[i].id != null && arr[i].id != undefined && arr[i].id != ""){
	      if(parseInt(arr[i].id) == parseInt(sid)){
	        return arr[i];
	      }
	     }
	  }
	  return null;
	}
  	function mOvr(roww) {
		roww.style.cursor = "hand";
		if(roww != cur_row){
			roww.style.backgroundColor = "#FFF8C5";
		}
	}
	function mOut(roww) {
		roww.style.cursor = "default";
		roww.style.backgroundColor = "#F9F8E8";
		if(cur_row != null){
			cur_row.style.backgroundColor = "#EEE999";
		}
	}
	function saveResource(){
		$('prompt_info').innerHTML = "操作正在执行...";
		$("err_msg_apsn").innerHTML = "";
		if(cur_role == 0){
			$("err_msg_apsn").innerHTML = "请选择一个管理员。";
			return;
		}
		var allNodes =resourceTree.getNodesAll();
		var isstring = "";
  		if(allNodes.length>0){
  			for(var i = 0;i<allNodes.length;i++){
  				isstring += "," + allNodes[i].id;
  			}
  			isstring = isstring.substring(1);
  			var paras = "action=save&roleId=" + cur_role + "&idstring=" + isstring;
  			new Ajax("roleResource.was",{postBody:paras,onComplete:getRoleResources,encoding:'UTF-8'}).request();
  		}else{
  			var paras = "action=save&roleId=" + cur_role + "&idstring=";
  			new Ajax("roleResource.was",{postBody:paras,onComplete:getRoleResources,encoding:'UTF-8'}).request();
  		}
	}
  </script>
	<%
				DbHibernateService service = (DbHibernateService) ContextBean
				.getBean("dbHibernateService");
		List roles = service.find("from Role");
	%>
	<%!void recursiveXytree(RightTree antiaTree, int i, JspWriter out)
			throws Exception {
		if (antiaTree.hasChild()) {

			TreeNode node = antiaTree.getNode();
			printTree(i, node, out);
			//System.out.println(node.getCode());
			List cs = antiaTree.getChild();
			++i;
			for (Iterator it = cs.iterator(); it.hasNext();) {
				Object c = it.next();
				RightTree tree = (RightTree) c;
				recursiveXytree(tree, i, out);
			}
		} else {
			TreeNode node = antiaTree.getNode();
			printTree(i, node, out);
		}
	}

	void printTree(int i, TreeNode node, JspWriter out) throws Exception {
		if (i == 0) {
			StringBuffer sb = new StringBuffer();
			sb.append("var resourceTree = new xyTree.DivTree('").append(
					node.getName()).append("');\n");
			out.print(sb.toString());
		} else {
			StringBuffer sb = new StringBuffer();
			sb.append("node").append(i).append(" = new xyTree.Node('").append(
					node.getName()).append("');\n");
			out.print(sb.toString());
			sb = new StringBuffer("node").append(i).append(".id=").append(
					node.getId()).append(";\n");
			out.print(sb.toString());
			if (i == 1) {
				sb = new StringBuffer("resourceTree.add(node").append(i)
						.append(");\n");
				out.print(sb.toString());
			} else {
				sb = new StringBuffer("node").append(i - 1).append(".add(node")
						.append(i).append(");\n");
				out.print(sb.toString());
			}
		}
	}%>
	<body><br>
		<h4 style=" color: #123234">
			Role and resources relationship operation page.
		</h4>
		<br>
		<table width="800">
			<tr>
				<td valign="top" width="400">
					<span class="role_th">Roles list:</span>
					<br>
					<br>
					<table cellspacing="1" cellpadding="3" class="table_role">
						<tr>
							<td>
								<span class="role_th">Role name</span>
							</td>
							<td>
								<span class="role_th">Role code</span>
							</td>
							<td>
								<span class="role_th">Organization</span>
							</td>
						</tr>
						<%
								if (roles != null && roles.size() > 0) {
								for (int i = 0; i < roles.size(); i++) {
									Role role = (Role) roles.get(i);
						%>
						<tr onclick="changeRole(<%=role.getId()%>,this)" onmouseover="mOvr(this)" onmouseout="mOut(this)">
							<td>
								<%=role.getName()%>
							</td>
							<td>
								<%=role.getCode()%>
							</td>
							<td>
								<%=role.getOrganName()%>
							</td>
						</tr>
						<%
							}
							}
						%>
					</table>
				</td>
				<%
					AntiaPoolHelper pool = (AntiaPoolHelper) ContextBean.getBean("antiaPoolHelper");
					ResourceTreeBuilder treeBuilder = (ResourceTreeBuilder) ContextBean
							.getBean("resourceTreeBuilder");
					RightTree tree = treeBuilder.buildTree(pool.getAllResources());
				%>

				<td valign="top" width="400">
					<span class="role_th">Right tree and Resources of had right.</span>
					<br>
					<br>
					<!-- 输出资源树 -->
					<script type="text/javascript">
    		//定义树使用命名空间
			//var allResources = new xyTree.DivTree('所有资源');
			//5个临时变量
			var node1,node2,node3,node4,node5,node6;
			<%
				try{
					recursiveXytree(tree,0,out);
				}catch(Exception e){
					out.print("document.write('Sorry,here occur a error.');");
				}
			%>
    		</script>
					<div id="rigth_tree">

					</div>
					<!-- 初始化资源树 -->
					<script type="text/javascript">
    			document.getElementById('rigth_tree').appendChild(resourceTree.div);
  				resourceTree.init(); //这是自定义回调函数
    		</script>
				</td>
			</tr>
			<tr>
				<td colspan="2">
					<span id="prompt_info" style="padding-left: 77px"></span>
					<br>
					<span style="padding-left: 237px"><input type="button" value="save resources" onclick="saveResource();"/></span>
					<span style="color: red;font-size: 12px;padding-left: 37px;font-weight: bold;" id="err_msg_apsn"></span>
		</table>
	</body>
</html>
